package com.kpx.app.wigdet;

import android.content.Context;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.kpx.app.bean.Goods;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.kpx.app.R;
import com.kpx.app.utils.UniversualImageLoaderUtils;

/**
 * 商品详情Dialog
 *
 * @author
 */
public class DialogGoodDetail extends BaseDialog implements View.OnClickListener {

    private OnAddButtonClickListener onAddButtonClickListener;
    private OnMinusButtonClickListener onMinusButtonClickListener;
    /**
     * 添加按钮
     */
    private ImageButton btnAdd;
    /**
     * 删减按钮
     */
    private ImageButton btnMinus;
    /**
     * 选中的商品信息对象
     */
    private Goods mGoods;
    /**
     * 选中的商品数量显示View
     */
    private TextView mSelectCountView;

    /**
     * 弹出商品详情构造函数
     *
     * @param context                    上下文
     * @param goods                      商品信息
     * @param onAddButtonClickListener   添加按钮回调
     * @param onMinusButtonClickListener 删减按钮回调
     */
    public DialogGoodDetail(Context context, Goods goods, OnAddButtonClickListener onAddButtonClickListener, OnMinusButtonClickListener onMinusButtonClickListener) {
        super(context);
        mGoods = goods;
        setOnAddButtonClickListener(onAddButtonClickListener);
        setOnMinusButtonClickListener(onMinusButtonClickListener);
        setContent();
    }

    private void setContent() {
        setContentView(R.layout.good_detail_alert_dialog_layout);
        final ImageView goodPicture = (ImageView) findViewById(R.id.good_detail_big_picture);
        if (null != mGoods) {
            ImageLoader.getInstance().displayImage(mGoods.getImgs(), goodPicture, UniversualImageLoaderUtils.getTopRoundCornerImageOptions(15));
            TextView goodPrice = (TextView) findViewById(R.id.good_detail_price_view);
            goodPrice.setText(String.valueOf(mGoods.getPrice()));
            TextView goodName = (TextView) findViewById(R.id.good_detail_name_view);
            goodName.setText(mGoods.getGoodsName());
            mSelectCountView = (TextView) findViewById(R.id.good_detail_count_view);
            mSelectCountView.setText(String.valueOf(mGoods.getSelectCount()));
            btnAdd = (ImageButton) findViewById(R.id.good_detail_add_btn);
            btnMinus = (ImageButton) findViewById(R.id.good_detail_minus_btn);
            btnAdd.setOnClickListener(this);
            btnMinus.setOnClickListener(this);
            findViewById(R.id.good_detail_close_btn).setOnClickListener(this);
        }
    }

    /**
     * 确定
     */
    public void setOnAddButtonClickListener(OnAddButtonClickListener listener) {
        this.onAddButtonClickListener = listener;
    }

    /**
     * 取消
     */
    public void setOnMinusButtonClickListener(OnMinusButtonClickListener listener) {
        this.onMinusButtonClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.good_detail_add_btn:
                mGoods.setSelectCount(mGoods.getSelectCount() + 1);
                mSelectCountView.setText(String.valueOf(mGoods.getSelectCount()));
                if (onAddButtonClickListener != null)
                    onAddButtonClickListener.onAddButtonClick(mGoods);
                break;
            case R.id.good_detail_minus_btn:
                if (mGoods.getSelectCount() > 0) {
                    mGoods.setSelectCount(mGoods.getSelectCount() - 1);
                }
                mSelectCountView.setText(String.valueOf(mGoods.getSelectCount()));
                if (onMinusButtonClickListener != null)
                    onMinusButtonClickListener.onMinusButtonClick(mGoods);
                break;
            case R.id.good_detail_close_btn:
                dismiss();
                break;
        }
    }

    public interface OnAddButtonClickListener {
        void onAddButtonClick(Goods goods);
    }

    public interface OnMinusButtonClickListener {
        void onMinusButtonClick(Goods goods);
    }
}
